package com.xquant.transaction_management_system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xquant.transaction_management_system.mapper.AccountMapper;
import com.xquant.transaction_management_system.mapper.TransactionControlMapper;
import com.xquant.transaction_management_system.model.PO.Account;
import com.xquant.transaction_management_system.model.PO.FinancialInstruments;
import com.xquant.transaction_management_system.model.PO.TransactionControl;
import com.xquant.transaction_management_system.model.VO.TransactionControlSearchVo;
import com.xquant.transaction_management_system.service.AccountService;
import com.xquant.transaction_management_system.service.TransactionControlService;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TransactionControlServiceImpl extends ServiceImpl<TransactionControlMapper, TransactionControl> implements TransactionControlService {
    @Override
    public IPage<TransactionControl> selectPage(Page<TransactionControl> pageParam, TransactionControlSearchVo transactionControlSearchVo) {
        QueryWrapper<TransactionControl> queryWrapper = new QueryWrapper<>();
        if (transactionControlSearchVo==null){
            IPage<TransactionControl> transactionControlPage = baseMapper.selectPage(pageParam, queryWrapper);
            return transactionControlPage;
        }
        if (transactionControlSearchVo.getTransactionAccount()!=null){
            queryWrapper.eq("transaction_account",transactionControlSearchVo.getTransactionAccount());
        }
        if (transactionControlSearchVo.getTransactionDate()!=null){
            queryWrapper.eq("transaction_date",transactionControlSearchVo.getTransactionDate());
        }else {
            Date date = new Date();
            DateFormat df = DateFormat.getDateInstance();
            String t_date = df.format(date).toString();
            queryWrapper.eq("transaction_date",t_date);
        }
        IPage<TransactionControl> controlPage = baseMapper.selectPage(pageParam, queryWrapper);
        return controlPage;
    }

    @Override
    public IPage<TransactionControl> selectByPage(Page<TransactionControl> pageParam) {
        QueryWrapper<TransactionControl> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
//        DateFormat df = DateFormat.getDateInstance();
//        String t_date = df.format(date).toString();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String format = f.format(date);
        System.out.println(format);
        queryWrapper.eq("transaction_date",format);
        IPage<TransactionControl> controlPage = baseMapper.selectPage(pageParam, queryWrapper);
        return controlPage;
    }
}
